# -*- coding: UTF-8 -*-

# coding=utf-8

#  注释

# 数据持久性模块
import pickle

# 系统参数模块,获取系统信息 os -> window系统 , prox -> linux.unix系统,  o,x -> mac系统
import os

# 时间模块
import time
import datetime
import calendar

# 集合模块
import collections

# 非常强大的图像处理工具库  cmd 执行 pip install Pillow 安装此包
from PIL import Image

'''
注释
'''

"""
注释
"""

print("hello 你好!")

input = int(input("请输入一个数:"))

index = 25
if input/3 == index:
    print(input,"是3的倍数")
elif input**2 == index:
    print(input, "二次方是", input**2 )
else: print(input)


list_ = [1, 2, 3, 4, 5, 6]
print(list_[0] is 1)


#分解质因数
i = 2
while input != 1:
    if input % i == 0:
       print(i, "x")
       input = input / i
    else:
        i += 1

# 找字符串中单词个数

str1 = "  aafevgh asfderhhrdbr  闻风丧胆发@W我我r  "
#str1 = str.strip();
j = 0
count = 0

while j < len(str1):
    while str1[j] != " ":
        count += 1

        j += 1
        if j == len(str1):
            break;
    else:
        j += 1
        if j == len(str1):
            break;
print("count = ", count)

# 迭代器 arr = ( x for x in [1,2,3,4,5] )
arr = iter("qwertyui")
print(next(arr), next(arr), next(arr), next(arr), next(arr), next(arr))

# 定义简单方法
def myfunc(name , *args):
    print("你的名字是:", name)
    print("参数数组:", args)

myfunc("123", "6","7","8")

# 数据结构: 字典
dir = {}
dir.setdefault("1", "1")
dir.setdefault("2", "2")
dir.setdefault("3", "3")
print("dir = ", dir)
print("dir.get(2) = ", dir.get("2"))

# 装饰器函数
#@outer  相当于 say_up = outer(say)  outer方法需要拿到上面去
def say(name, age):
    print("My name is %s, and i'm %d years old!" % (name, age))

say("uu", 18)

def outer(func):
    def inner(*args, **kwargs):
        #if xxxx
        print("通用装饰器函数")
        func(*args, **kwargs)
    return inner

say_up = outer(say)

say_up("uu", -18)


# 文件读取与写入  r表示对斜杠转义
print("*******************读取文件**********************")
path = r"E:\python\test\file.txt"

with open(path, "w", encoding="utf-8", errors="ignore") as file:
    file.write("what's your name ?")

# 简写方法, 普通写法需要 file.close() 关闭文件
with open(path, "r", encoding="utf-8", errors="ignore") as file:
    print(file.read(100))

with open(path, "a", encoding="utf-8", errors="ignore") as file:
    file.write("\nmy name is meixl")

try:
    print("老方法读取:")

    file = open(path, "r", encoding="utf-8", errors="ignore")

    line = file.readline(4)
    str = "";
    while line:
        str += line
        line = file.readline(4)
    print(str)
except BaseException as e:
    print("读取错误")
finally:
    if file:
        file.close()

# 二进制文件 读写
print("*******************读取文件二进制数据**********************")
path2 = r"E:\python\test\file2.txt"
with open(path2, "wb") as file2:
    file2.write("二进制文件读写!".encode("gbk"))

with open(path2, "rb") as file2:
    data = file2.read()
    strData = data.decode("gbk");
    print("读取的二进制数据,未解码的bytes:", data)
    print("读取的二进制数据,解码后变成的字符串:", strData)

# 读写list列表或元组到文件
print("*******************读写list列表到文件**********************")
path3 = r"E:\python\test\file3.txt"

list_ = [1, 2, 3, 4, 5, "meixl"]
with open(path3, "wb") as file2:
    pickle.dump(list_, file2)

with open(path3, "rb") as file2:
    print("读取到内容:", pickle.load(file2))

# 获取系统文件目录
print("*******************获取系统文件目录**********************")
path4 = r"E:\python\test"
def getFile(path):
    listDir = os.listdir(path)
    for fileName in listDir:
        p = os.path.join(path, fileName)
        if os.path.isfile(p):
            print(p, "文件")
        elif os.path.isdir(p):
            print(p, "目录")
            getFile(p)

getFile(path4)

# 时间
print("*******************时间**********************")
time.time()
print(time.strftime("%Y-%m-%d %H:%M:%S"))

print(datetime.datetime.strftime( datetime.datetime.now(), "%Y-%m-%d %H:%M:%S"))

print(calendar.month(2020, 5))

# 图像处理
print("*******************图像处理**********************")
image = Image.open(r"C:\Users\meixl\Downloads\5.jpg")
print(image.format, image.size, image.mode)


# 列表去重
print("*******************列表去重**********************")
list1 = [1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6]

list2 = list(set(list1))
print("list2 = ", list2)
print("*******************列表去重**********************")

# list 的 append 用法
print("*******************list 的 append 用法**********************")
dataAppend = []
for i in range(10):
    dataAppend.append(i)
print("dataAppend = ", dataAppend)
print("*******************list 的 append 用法**********************")